package com.umfwechat.util;

import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 
 * @author WL
 *
 */
public class JedisFactory {
	
	private static Logger logger = LoggerFactory.getLogger(JedisFactory.class);
	
	private static JedisPool jedisPool = null;
//	private static Properties redis_Properties = PropertyUtil.getProperties("umpaycache.properties");
//	private static String IP = "10.10.77.76";
//	private static int PORT = Integer.parseInt("6379");
//	private static int MAX_ACTIVE = Integer.parseInt("5");
//	private static int MAX_IDLE = Integer.parseInt("5");
//	private static int MAX_WAIT = Integer.parseInt("300000");
//	private static boolean TEST_ON_BORROW = Boolean.parseBoolean("true");
	private static Properties redis_Properties = PropertyUtil.getProperties("umpaycache.properties");
	private static String IP = StringUtil.trim(redis_Properties.getProperty("redis_ip"));
	private static int PORT = Integer.parseInt(StringUtil.trim(redis_Properties.getProperty("redis_port")));
	private static int MAX_ACTIVE = Integer.parseInt(StringUtil.trim(redis_Properties.getProperty("redis_port")));
	private static int MAX_IDLE = Integer.parseInt(StringUtil.trim(redis_Properties.getProperty("max_idle_num")));
	private static int MAX_WAIT = Integer.parseInt(StringUtil.trim(redis_Properties.getProperty("max_wait")));
	private static boolean TEST_ON_BORROW = Boolean.parseBoolean(StringUtil.trim(redis_Properties.getProperty("test_on_borrow")));
	
	/**
	 * jedis连接池初始化
	 */
	private static void poolInit(){
//		synchronized(jedisPool){
			if(jedisPool==null){
				JedisPoolConfig config = new JedisPoolConfig();
				logger.info("IP:"+IP+"PORT:"+PORT+"MAX_ACTIVE:"+MAX_ACTIVE+"MAX_IDLE:"+MAX_IDLE+"MAX_WAIT:"+MAX_WAIT+"TEST_ON_BORROW:"+TEST_ON_BORROW);
		        config.setMaxTotal(MAX_ACTIVE);
		        config.setMaxIdle(MAX_IDLE);
		        config.setMaxWaitMillis(MAX_WAIT);
		        config.setTestOnBorrow(TEST_ON_BORROW);
		        jedisPool = new JedisPool(config, IP, PORT);
			}
		}
//	}
	
	/**
	 * 获取jedis单个实例
	 * @return
	 */
	public static Jedis getJedis(){
		poolInit();
		logger.info(jedisPool.toString());
		return jedisPool.getResource();
	}
}
